<template>
  <div id="detail">
    <detail-nav-bar class="dnb" @titleClick="titleClick" ref="nav" />

    <div class="wrapper">
      <scroll
        class="content"
        ref="scroll"
        :probe-type="3"
        :pull-up-load="true"
        @scroll="contentScroll"
      >
        
        <detail-swiper :topImages="topImages" @detailSwiperLoad="detailSwiperLoad"/>
        <detail-base-info :goods="goods" />
        <detail-shop-info :shop="shop" />
        <detail-goods-info
          :detailInfo="detailInfo"
          @detailImageLoad="detailImageLoad"
        />
        <detail-param-info ref="param" :paramInfo="paramInfo" />
        <detail-comment-info ref="comment" :commentInfo="commentInfo" />
        <good-list ref="recommend" :goods="recommends" class="good-list" />
      </scroll>
    </div>
    <detail-bottom-bar @addToCart="addToCart"/>

    <back-top @click.native="backClick" v-show="isShowBackTop" />
    <!-- <toast :message="message" :show="show"/> -->
  </div>
</template>

<script>
import DetailNavBar from "./childComps/DetailNavBar.vue";
import DetailSwiper from "./childComps/DetailSwiper.vue";
import DetailBaseInfo from "./childComps/DetailBaseInfo.vue";
import DetailShopInfo from "./childComps/DetailShopInfo.vue";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo.vue";
import DetailParamInfo from "./childComps/DetailParamInfo.vue";
import DetailCommentInfo from "./childComps/DetailCommentInfo.vue";
import DetailBottomBar from "./childComps/DetailBottomBar.vue";

import Scroll from "../../components/common/scroll/Scroll.vue";
import GoodList from "../../components/content/goods/GoodsList.vue";
import BackTop from "../../components/content/backtop/BackTop.vue";
// import Toast from "../../components/common/toast/Toast.vue"

import {
  getDetail,
  Goods,
  Shop,
  GoodsParam,
  getRecommend,
} from "../../network/detail";
import { debounce } from "../../common/utils";
import { itemListenerMixin } from "../../common/mixin";
import { mapActions } from 'vuex'

export default {
  name: "Detail",
  mixins: [itemListenerMixin],

  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0,
      isShowBackTop: false,
      // message: '',
      // show: false,
    };
  },

  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodList,
    DetailBottomBar,
    BackTop,
    // Toast
  },

  created() {
    // 1.保存存入的iid
    this.iid = this.$route.params.iid;

    // 2.根据iid请求详细的数据
    getDetail(this.iid).then((res) => {
      // console.log(res);
      // 1.获取数据
      const data = res.result;
      this.topImages = data.itemInfo.topImages;

      // 2.获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );

      // 3.创建店铺信息的对象
      this.shop = new Shop(data.shopInfo);

      // 4.保存商品的详细数据
      this.detailInfo = data.detailInfo;

      // 5.获取参数信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );

      // 6.取出评论的信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }

      // 获取的值不对，图片没有加载完全
      // this.$nextTick(() => {
      // this.themeTopYs = [];

      // this.themeTopYs.push(0);
      // this.themeTopYs.push(this.$refs.param.$el.offsetTop);
      // this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      // this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);

      // console.log(this.themeTopYs);
      // });
    });

    // 3.请求推荐数据
    getRecommend().then((res) => {
      this.recommends = res.data.list;
    });

    // 4.为getThemeTopY赋值,对赋值操作进行防抖
    this.getThemeTopY = debounce(() => {
      this.themeTopYs = [];

      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.param.$el.offsetTop);
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
      this.themeTopYs.push(Number.MAX_VALUE);

      // console.log(this.themeTopYs);
    }, 100);
  },

  mounted() {
    //1. 图片加载完成的监听事件
    const refresh = debounce(this.$refs.scroll.refresh, 150);
    this.$bus.$on("itemImageLoad", () => {
      refresh();
    });

  },
  destroyed() {
    this.$bus.$off("itemImageLoad", this.itemImgListener);
  },

  methods: {
    // 将store中的actions中的方法映射到这里
    ...mapActions(['addCart']),

    detailSwiperLoad() {
      this.$refs.scroll.refresh()
    },
    detailImageLoad() {
      this.$refs.scroll.refresh()
      this.getThemeTopY();
    },
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200);
    },
    contentScroll(position) {
      // 1.获取y值
      const positionY = -position.y;
      let length = this.themeTopYs.length;

      // 2.positionY和主题中的值进行对比
      for (let i = 0; i < length; i++) {
        if (
          this.currentIndex != i &&
          ((i < length - 1 &&
            positionY >= this.themeTopYs[i] &&
            positionY < this.themeTopYs[i + 1]) ||
            (i === length - 1 && positionY >= this.themeTopYs[i]))
        )
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
      }

      // 3.判断backTop是否显示
      this.isShowBackTop = -position.y > 1000;
    },
    backClick() {
      this.$refs.scroll.scrollTo(0, 0);
    },
    addToCart() {
      // 1.获取购物车需要的信息
      const product = {}
        product.image = this.topImages[0];
        product.title = this.goods.title;
        product.desc = this.goods.desc;
        product.price = this.goods.realPrice;
        product.iid = this.iid;
        // product.count = 0

      // 2.将商品添加到购物车
      // 使用映射的actions中的addCart方法
      this.addCart(product).then(res => {
        // this.show = true;
        // this.message = res;

        // setTimeout(() => {
        //   this.show = false;
        //   this.message = '';
        // },1000)
        this.$toast.shows(res,1500)
      })

      // this.$store.commit('addCart', product)
      // this.$store.dispatch('addCart', product).then(res => {
      //   console.log(res);
      // })

      // 3.加入购物车成功
    }

  },
};
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  /* height: 100vh; */
}

.dnb {
  position: relative;
  z-index: 9;
  background-color: #fff;
}

.wrapper {
  height: 100vh;
  position: absolute;
  left: 0;
  top: 0;
  overflow: hidden;
  width: 100%;
  background-color: #fff;
}

.content {
  height: calc(100% - 93px);
  overflow: hidden;
  position: absolute;
  top: 44px;
  left: 0;
  right: 0;
  z-index: 99;
}

.good-list {
  margin-top: 300px;
}
</style>